﻿using LPY.LeHealth.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPY.LeHealth.DAL
{
    using ServiceStack.OrmLite;

    public class UserDAL:GenericBaseDAL<UserDTO>
    {
        public long Add(UserDTO usr)
        {
            var uid = DbConnection.Insert<UserDTO>(usr, selectIdentity: true);
            return uid;
        }

        public UserDTO Get(string name,string pwd,int roleId=0)
        {
            using (var cnn=DbConnection)
            {
                //查询表达式
                var sql = cnn.From<UserDTO>()
                    .Where(m => m.Name == name && m.Password == pwd && m.RoleId==roleId);
                
                //查询
                var rtv = cnn.Single(sql);
                return rtv;
            }
        }

        public UserDTO GetByPhoneNumber(string phone)
        {
            using (var cnn=DbConnection)
            {
                var sql= cnn.From<UserDTO>()
                    .Where(m => m.PhoneNumber == phone);
                return cnn.Single(sql);
            }
        }

        public UserDTO GetByPhoneAndPwd(string phone,string pwd)
        {
            using (var cnn=DbConnection)
            {
                var sql = cnn.From<UserDTO>()
                    .Where(m => m.PhoneNumber == phone && m.Password == pwd);
                return cnn.Single(sql);
            }
        }
        /// <summary>
        /// 根据用电话验证此电话是否已经注册
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public List<UserDTO> GetByPhone(string phone)
        {
            using (var cnn = DbConnection)
            {
                var sql = cnn.From<UserDTO>()
                    .Where(m => m.PhoneNumber == phone);
                return cnn.Select<UserDTO>(sql);
            }
        }
		/// <summary>
		/// 修改用户首单字段信息
		/// </summary>
		public bool UpdateUserByUid(UserDTO user)
		{
			var cnn = DbConnection;
			var result = cnn.Update<UserDTO>(user);
			return result > 0;
		}

        /// <summary>
        /// 根据Id获取用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public UserDTO GetById(int uid)
        {
            using (var cnn=DbConnection)
            {
                var sql = cnn.From<UserDTO>()
                    .Where(m => m.Id == uid);
                return cnn.Single(sql);
            }
        }



        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateName(int Id, string name)
        {
            using (var cnn = DbConnection)
            {
                var result = cnn.UpdateOnly<UserDTO>(() => new UserDTO { Name = name }, where: m => m.Id == Id);
                return result > 0;
            }
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateTrueName(int Id, string name)
        {
            using (var cnn = DbConnection)
            {
                var result = cnn.UpdateOnly<UserDTO>(() => new UserDTO { TrueName = name }, where: m => m.Id == Id);
                return result > 0;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool Updapwd(int Id, string Password)
        {
            using (var cnn = DbConnection)
            {
                var result = cnn.UpdateOnly<UserDTO>(() => new UserDTO{Password =Password}, where: m => m.Id == Id);
                return result > 0;
            }
        }

        


    }
}
